/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.hhq.sso.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hhq.sso.common.service.BaseServiceImpl;
import com.hhq.sso.system.entity.Dept;
import com.hhq.sso.system.entity.Role;
import com.hhq.sso.system.entity.Tenant;
import com.hhq.sso.system.mapper.DeptMapper;
import com.hhq.sso.system.mapper.RoleMapper;
import com.hhq.sso.system.mapper.TenantMapper;
import com.hhq.sso.system.service.ITenantService;

import cn.hutool.core.util.IdUtil;
import lombok.AllArgsConstructor;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
@AllArgsConstructor
public class TenantServiceImpl extends BaseServiceImpl<TenantMapper, Tenant> implements ITenantService {

    private final RoleMapper roleMapper;
    private final DeptMapper deptMapper;

    @Override
    public IPage<Tenant> selectTenantPage(IPage<Tenant> page, Tenant tenant) {
        return page.setRecords(baseMapper.selectTenantPage(page, tenant));
    }

    @Override
    public Tenant getByTenantId(String tenantId) {
        return getOne(Wrappers.<Tenant>query().lambda().eq(Tenant::getTenantId, tenantId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveTenant(Tenant tenant) {
        if (Func.isEmpty(tenant.getId())) {
            List<Tenant> tenants = baseMapper
                .selectList(Wrappers.<Tenant>query().lambda().eq(Tenant::getIsDeleted, BladeConstant.DB_NOT_DELETED));
            String tenantId = IdUtil.getSnowflake(8).nextIdStr();
            tenant.setTenantId(tenantId);
            // 新建租户对应的默认角色
            Role role = new Role();
            role.setTenantId(tenantId);
            role.setParentId(0L);
            role.setRoleName("管理员");
            role.setRoleAlias("admin");
            role.setSort(2);
            role.setIsDeleted(0);
            roleMapper.insert(role);
            // 新建租户对应的默认部门
            Dept dept = new Dept();
            dept.setTenantId(tenantId);
            dept.setParentId(0L);
            dept.setDeptName(tenant.getTenantName());
            dept.setFullName(tenant.getTenantName());
            dept.setSort(2);
            dept.setIsDeleted(0);
            deptMapper.insert(dept);
            return super.saveOrUpdate(tenant);
        }
        return super.saveOrUpdate(tenant);
    }
}
